package com.rhino.xchat.ui.activity

import android.content.Intent
import com.rhino.base.ui.activity.BaseActionBarActivity
import com.rhino.xchat.R
import com.rhino.xchat.App
import com.rhino.xchat.constant.Cache
import com.rhino.xchat.constant.RouteConstant
import com.rhino.xchat.databinding.ActivityWelcomeBinding
import com.therouter.TheRouter
import com.therouter.router.Navigator
import com.therouter.router.interceptor.NavigationCallback

/**
 * @author LuoLin
 * @since Create on 2024/9/29.
 */
class WelcomeActivity : BaseActionBarActivity<ActivityWelcomeBinding>() {

    private val delayToNextRunnable by lazy { Runnable { toNextPage() } }

    override fun initTitle(): String? {
        return null
    }

    override fun getLayoutResId(): Int {
        return R.layout.activity_welcome
    }

    override fun initView() {
        rootBinding.defaultActionBar.setBottomLineVisible(false)
        rootBinding.defaultActionBar.setBottomLineShadowVisible(false)
        showDebuggerView(false)
    }

    override fun onResume() {
        super.onResume()
        // 解决前后台切换时打开闪屏页
        if (!isTaskRoot
            && intent.hasCategory(Intent.CATEGORY_LAUNCHER)
            && intent.action != null
            && intent.action.equals(Intent.ACTION_MAIN)
        ) {
            finish()
            return
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        thisHandler.removeCallbacks(delayToNextRunnable)
    }

    override fun registerLiveData() {
        thisHandler.postDelayed(delayToNextRunnable, 1000)
    }

    private fun toNextPage() {
        App.init(application)
        if (!Cache.hasLogin()) {
            TheRouter.build(RouteConstant.LOGIN_ACTIVITY)
                .withInAnimation(R.anim.welcome_activity_open_enter)
                .withOutAnimation(R.anim.welcome_activity_close_exit)
                .navigation(callback = object : NavigationCallback() {
                    override fun onArrival(navigator: Navigator) {
                        finish()
                    }
                })
            return
        }

        TheRouter.build(RouteConstant.MAIN_ACTIVITY)
            .withInAnimation(R.anim.welcome_activity_open_enter)
            .withOutAnimation(R.anim.welcome_activity_close_exit)
            .navigation(callback = object : NavigationCallback() {
                override fun onArrival(navigator: Navigator) {
                    finish()
                }
            })

    }
}